<template>
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>

    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="saveAssignRoles">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/settings'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      // el-checkbox组件 label后面的值
      // 如果当前复选框选中就把label后面的值存到这里
      roleIds: [],
      // 保存角色列表
      list: []

    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    async saveAssignRoles() {
      try {
        const res = await assignRoles({ id: this.id, roleIds: this.roleIds })
        console.log('assignRoles', res)
        // 保存数据
        this.$emit('success')
      } catch (err) {
        console.log('assignRoles', err)
      }
    },
    async loadRoles() {
      try {
        const res = await getRoles({ page: 1, pagesize: 100 })
        console.log('getRoles', res)
        // 保存数据
        this.list = res.data.rows
        const info = await getUserDetailById(this.id)
        this.roleIds = info.data.roleIds
      } catch (err) {
        console.log('getRoles', err)
      }
    },
    closeDialog() {
      this.$emit('close')
    }
  }
}
</script>
